package cn.edu.neusoft.neufooddemo.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.adapter.FoodListAdapter;
import cn.edu.neusoft.neufooddemo.bean.FoodInfo;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;



public class SearchFragment2 extends Fragment implements RetrofitListener<List<FoodInfo>>{

    private EditText edit_search;
    private TextView search;
    private RecyclerView search_rv;
    private View view = null;
    private FoodListAdapter adapter;
    private FoodModel foodModel;

    public SearchFragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tab031, container, false);
        initViews();
        initEvents();
        return view;
    }

    private void initViews(){
        edit_search = (EditText)view.findViewById(R.id.edit_search);
        search_rv = (RecyclerView)view.findViewById(R.id.search_rv);
        search_rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        search = (TextView)view.findViewById(R.id.search);
    }

    private void initEvents(){
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
            }
        });
    }

    private void search(){
        foodModel = new FoodModel();
       foodModel.getFoodBySearch(edit_search.getText().toString(),this);
    }

    @Override
    public void onSuccess(List<FoodInfo> foodInfos, int flag) {
        adapter = new FoodListAdapter(getActivity(), foodInfos, R.layout.cardview_food);
        search_rv.setAdapter(adapter);
    }

    @Override
    public void onFail() {

    }
}
